package com.cyh.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//需要在spring-mvc中配置它
public class MyInterceptor2 implements HandlerInterceptor {

    //在目标方法执行之前执行，例：TargetController.show方法之前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle。。。222222");

        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    //在目标方法执行之后，视图对象返回之前执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        //通过modelAndView改变数据和视图
        System.out.println("postHandle。。。2222222222");
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    //在整个流程执行完成后执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //用的比较少，一些释放操作
        System.out.println("afterCompletion。。。222222");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
